<!DOCTYPE html>
<!-- saved from url=(0052)http://getbootstrap.com/docs/4.0/examples/dashboard/ -->
<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <!-- Bootstrap core CSS -->
    <link th:href="@{../asserts/css/bootstrap.min.css}" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link th:href="@{../asserts/css/dashboard.css}" rel="stylesheet">
    <style type="text/css">
        @-webkit-keyframes chartjs-render-animation {
            from {
                opacity: 0.99
            }
            to {
                opacity: 1
            }
        }

        @keyframes chartjs-render-animation {
            from {
                opacity: 0.99
            }
            to {
                opacity: 1
            }
        }

        .chartjs-render-monitor {
            -webkit-animation: chartjs-render-animation 0.001s;
            animation: chartjs-render-animation 0.001s;
        }

        .hidden {
            display: none !important;
        }
    </style>
</head>

<body>
<div th:replace="commons/bar::topbar"></div>
<div class="container-fluid">
    <div class="row">
        <!--引入侧边栏-->
        <div th:replace="commons/bar::sidebar(activeUri='purchaseOrderChecks')"></div>
        <main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
            <th:block th:if="${purchaseOrder ne null}">
                <h2>Подпись</h2>
            </th:block>
            <form th:action="@{/purchaseOrderCheck}" method="post">
                <input type="hidden" name="_method" value="put" th:if="${purchaseOrder ne null}">
                <div class="form-group hidden">
                    <label for="purchaseOrderNumber">Номер заказа на поставку</label>
                    <input type="text" class="form-control purchaseOrderNumber" id="purchaseOrderNumber"
                           name="purchaseOrderNumber" th:value="${purchaseOrder ne null}?${purchaseOrder.purchaseOrderNumber}">
                </div>
                <div class="form-group">
                    <label for="acceptor">Акцептант</label>
                    <select id="acceptor" class="form-control" name="acceptor" required>
                        <option th:value="${null}">Выбирать...</option>
                        <option th:each="acceptor : ${employeeUsersOfAcceptor}" th:text="${acceptor.username}"
                                th:value="${acceptor.employeeCode}"
                                th:selected="${purchaseOrder ne null}?${purchaseOrder.acceptor eq acceptor.employeeCode}"></option>
                    </select>
                </div>
                <div class="form-group">
                    <label for="orderer">Заказчик</label>
                    <select id="orderer" class="form-control" name="orderer" required disabled>
                        <option th:value="${null}">Выбирать...</option>
                        <option th:each="order : ${employeeUsersOfOrder}" th:text="${order.username}"
                                th:value="${order.employeeCode}"
                                th:selected="${purchaseOrder ne null}?${purchaseOrder.orderer eq order.employeeCode}"></option>
                    </select>
                </div>
                <div class="form-group">
                    <label for="purchaseOrderSupplier">Поставщик заказа на поставку</label>
                    <select id="purchaseOrderSupplier" class="form-control" name="purchaseOrderSupplier" required disabled>
                        <option th:value="${null}">Выбирать...</option>
                        <option th:each="supplierInfo : ${supplierInfos}" th:text="${supplierInfo.supplierName}"
                                th:value="${supplierInfo.supplierId}"
                                th:selected="${purchaseOrder ne null}?${purchaseOrder.purchaseOrderSupplier eq supplierInfo.supplierId}"></option>
                    </select>
                </div>
                <div class="form-group">
                    <label for="inputFourthLevel">Торговый адрес</label>
                    <select id="inputFourthLevel" class="form-control" name="shippingAddress" required disabled>
                        <option th:value="${null}">Выбирать...</option>
                        <option th:each="fourth : ${fourthLevel}" th:text="${fourth.fourthLevelName}"
                                th:value="${fourth.fourthLevelName}"
                                th:selected="${purchaseOrder ne null}?${fourth.getFourthLevelName() eq purchaseOrder.shippingAddress}"></option>
                    </select>
                </div>
                <div class="form-group">
                    <label for="warehouse">Склад</label>
                    <select id="warehouse" class="form-control" name="warehouse" required disabled>
                        <option th:value="${null}">Выбирать...</option>
                        <option th:each="warehouse : ${warehouseInfos}" th:text="${warehouse.warehouseName}"
                                th:value="${warehouse.warehouseCode}"
                                th:selected="${purchaseOrder ne null}?${purchaseOrder.warehouse eq warehouse.warehouseCode}"></option>
                    </select>
                </div>
                <div class="form-group">
                    <label for="datetimepicker">Дата заказа на покупку</label>
                    <div class="input-append date" id="datetimepicker" data-date="" data-date-format="dd/mm/yyyy">
                        <input id="purchaseOrderDate" class="form-control" type="text"
                               th:value="${purchaseOrder ne null}?${#dates.format(purchaseOrder.purchaseOrderDate,'dd/MM/yyyy')}"
                               name="purchaseOrderDate" required disabled>
                        <span class="add-on"><i class="icon-th"></i></span>
                    </div>
                </div>
                <div class="form-group">
                    <label for="datetimepicker2">Дата прибытия заказа на покупку</label>
                    <div class="input-append date" id="datetimepicker2" data-date="" data-date-format="dd/mm/yyyy" disabled>
                        <input id="purchaseOrderArrivalDate" class="form-control" type="text"
                               th:value="${purchaseOrder ne null}?${#dates.format(purchaseOrder.purchaseOrderArrivalDate,'dd/MM/yyyy')}"
                               name="purchaseOrderArrivalDate" required disabled>
                        <span class="add-on"><i class="icon-th"></i></span>
                    </div>
                </div>
                <fieldset class="form-group" hidden>
                    <legend class="col-form-label pt-0">Заказ на поставку завершен</legend>
                    <div class="custom-control custom-radio custom-control-inline">
                        <input type="radio" id="purchaseOrderFinishedCondition1" name="purchaseOrderFinishedCondition"
                               class="custom-control-input" value="00">
                        <label class="custom-control-label" for="purchaseOrderFinishedCondition1">На ходу</label>
                    </div>
                    <div class="custom-control custom-radio custom-control-inline">
                        <input type="radio" id="purchaseOrderFinishedCondition2" name="purchaseOrderFinishedCondition"
                               class="custom-control-input" value="01">
                        <label class="custom-control-label" for="purchaseOrderFinishedCondition2">Отменено</label>
                    </div>
                    <div class="custom-control custom-radio custom-control-inline">
                        <input type="radio" id="purchaseOrderFinishedCondition3" name="purchaseOrderFinishedCondition"
                               class="custom-control-input" value="02" checked>
                        <label class="custom-control-label" for="purchaseOrderFinishedCondition3">Нет</label>
                    </div>
                </fieldset>
                <div class="form-group">
                    <label class="purchaseOrderRemarks">Примечания к заказу на покупку</label>
                    <input class="form-control" type="text" name="purchaseOrderRemarks" id="purchaseOrderRemarks"
                           th:value="${purchaseOrder ne null}?${purchaseOrder.purchaseOrderRemarks}" disabled>
                </div>
                <div class="form-group">
                    <label for="purchaser">Приобретатель</label>
                    <select id="purchaser" class="form-control" name="purchaser" disabled>
                        <option th:value="${null}">Выбирать...</option>
                        <option th:each="purchaser : ${employeeUsersOfPurchase}" th:text="${purchaser.username}"
                                th:value="${purchaser.employeeCode}"
                                th:selected="${purchaseOrder ne null}?${purchaseOrder.purchaser eq purchaser.employeeCode}"></option>
                    </select>
                </div>
                <div class="form-group">
                    <table class="table">
                        <thead>
                        <tr>
                            <th scope="col">Товарный штрих-код</th>
                            <th scope="col">Покупная цена</th>
                            <th scope="col">Количество</th>
                            <th scope="col">Количественная единица</th>
                            <th scope="col">Деньги</th>
                            <th scope="col">Срок хранения</th>
                            <th scope="col">Итоговая цена</th>
                        </tr>
                        </thead>
                        <tbody id="purchaseDetailsForm">
                        <th:block th:if="${purchaseOrder ne null}">
                            <tr th:each="purchaseDetailsInfo:${purchaseDetailsInfos}">
                                <td class="hidden">
                                    <label>
                                        <input type="text" class="form-control purchaseDetailsNumber"
                                               name="purchaseDetailsNumber"
                                               th:value="${purchaseOrder ne null}?${purchaseDetailsInfo.purchaseDetailsNumber}"
                                               disabled>
                                    </label>
                                </td>
                                <td class="hidden">
                                    <label>
                                        <input type="text" class="form-control purchaseOrderNumber"
                                               name="purchaseOrderNumber"
                                               th:value="${purchaseOrder ne null}?${purchaseDetailsInfo.purchaseOrderNumber}"
                                               disabled>
                                    </label>
                                </td>
                                <td>
                                    <label>
                                        <input type="text" class="form-control goodsBarCode" name="goodsBarCode"
                                               th:value="${purchaseOrder ne null}?${purchaseDetailsInfo.goodsBarCode}"
                                               disabled>
                                    </label>
                                </td>
                                <td>
                                    <label>
                                        <input type="text" class="form-control purchasePrice" name="purchasePrice"
                                               th:value="${purchaseOrder ne null}?${purchaseDetailsInfo.purchasePrice}"
                                               disabled>
                                    </label>
                                </td>
                                <td>
                                    <label>
                                        <input type="text" class="form-control purchaseDetailsQuantity"
                                               name="purchaseDetailsQuantity"
                                               th:value="${purchaseOrder ne null}?${purchaseDetailsInfo.purchaseDetailsQuantity}"
                                               disabled>
                                    </label>
                                </td>
                                <td>
                                    <label>
                                        <input type="text" class="form-control purchaseDetailsQuantityUnit"
                                               name="purchaseDetailsQuantityUnit"
                                               th:value="${purchaseOrder ne null}?${purchaseDetailsInfo.purchaseDetailsQuantityUnit}"
                                               disabled>
                                    </label>
                                </td>
                                <td>
                                    <label>
                                        <input type="text" class="form-control purchaseDetailsAmount"
                                               name="purchaseDetailsAmount"
                                               th:value="${purchaseOrder ne null}?${purchaseDetailsInfo.purchaseDetailsAmount}"
                                               disabled>
                                    </label>
                                </td>
                                <td>
                                    <label>
                                        <input type="text" class="form-control purchaseDetailsExpirationDate"
                                               name="purchaseDetailsExpirationDate"
                                               th:value="${purchaseOrder ne null}?${purchaseDetailsInfo.purchaseDetailsExpirationDate}"
                                               disabled>
                                    </label>
                                </td>
                                <td>
                                    <label>
                                        <input type="text" class="form-control purchaseDetailsTotalPrice"
                                               name="purchaseDetailsTotalPrice"
                                               th:value="${purchaseOrder ne null}?${purchaseDetailsInfo.purchaseDetailsTotalPrice}"
                                               disabled>
                                    </label>
                                </td>
                            </tr>
                        </th:block>

                        </tbody>
                    </table>
                </div>
                <div class="form-group">
                    <th:block th:if="${purchaseOrder ne null}">
                        <input type="submit" class="btn btn-primary" value="Представить" id="write-btn">
                    </th:block>
                </div>
            </form>
        </main>
    </div>
</div>
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script type="text/javascript" src="../../asserts/js/jquery-3.2.1.slim.min.js"></script>
<script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
<script type="text/javascript" src="../asserts/js/popper.min.js"></script>
<script type="text/javascript" src="../asserts/js/bootstrap.min.js"></script>
<script type="text/javascript" src="../asserts/js/bootstrap-datetimepicker.min.js"></script>
<script type="text/javascript" src="../asserts/js/locales/bootstrap-datetimepicker.ru.js"></script>

<!-- Icons -->
<script type="text/javascript" src="../asserts/js/feather.min.js"></script>
<script>
    feather.replace()
</script>

<!-- Graphs -->
<script type="text/javascript" src="../asserts/js/Chart.min.js"></script>


<script>
    $('#datetimepicker').datetimepicker({
        language: 'ru',
        minView: 'month',
        autoclose: true,
        setStartDate: new Date(),
        todayBtn: true,
        todayHighlight: true,
        keyboardNavigation: true
    }).on('changeDate', function (ev) {
        $(this).datetimepicker('hide');
    });

    $('#datetimepicker2').datetimepicker({
        language: 'ru',
        minView: 'month',
        autoclose: true,
        setStartDate: new Date(),
        todayBtn: true,
        todayHighlight: true,
        keyboardNavigation: true
    }).on('changeDate', function (ev) {
        $(this).datetimepicker('hide');
    });
</script>
<script>
    var httpUrl = "http://localhost:8088";

    function update() {
        var purchaseOrderNumber2 = $('#purchaseOrderNumber2').val();
        var purchaseOrderSupplier = $('#purchaseOrderSupplier option:selected').val();
        var shippingAddress = $('#inputFourthLevel option:selected').val();
        var warehouse = $('#warehouse option:selected').val();
        var purchaseOrderDate = $('#purchaseOrderDate').val();
        var purchaseOrderArrivalDate = $('#purchaseOrderArrivalDate').val();
        var purchaseOrderFinishedCondition = $("input[type='radio']:checked").val();
        var purchaseOrderRemarks = $("#purchaseOrderRemarks").val();
        var purchaser = $('#purchaser option:selected').val();

        var purchaseDetailsNumber;
        var elements = document.getElementsByName('purchaseDetailsNumber')[0];
        if (!elements) {
            purchaseDetailsNumber = elements;
        } else {
            purchaseDetailsNumber = '80000' + new Date().getMilliseconds();
        }
        var purchaseDetails = [];
        $('#purchaseDetailsForm tr').each(function (i, n) {
            purchaseDetails.push({
                purchaseDetailsNumber: purchaseDetailsNumber,
                purchaseOrderNumber: purchaseOrderNumber2,
                goodsBarCode: $(this).find('.goodsBarCode').val(),
                purchasePrice: $(this).find('.purchasePrice').val(),
                purchaseDetailsQuantity: $(this).find('.purchaseDetailsQuantity').val(),
                purchaseDetailsQuantityUnit: $(this).find('.purchaseDetailsQuantityUnit').val(),
                purchaseDetailsAmount: $(this).find('.purchaseDetailsAmount').val(),
                purchaseDetailsExpirationDate: $(this).find('.purchaseDetailsExpirationDate').val(),
                purchaseDetailsTotalPrice: $(this).find('.purchaseDetailsTotalPrice').val()
            });
        });

        var purchaseOrder = {"purchaseOrderNumber": purchaseOrderNumber2,
            "purchaseOrderSupplier": purchaseOrderSupplier,
            "shippingAddress": shippingAddress,
            "warehouse": warehouse,
            "purchaseOrderDate": purchaseOrderDate,
            "purchaseOrderArrivalDate": purchaseOrderArrivalDate,
            "purchaseOrderFinishedCondition": purchaseOrderFinishedCondition,
            "purchaseOrderRemarks": purchaseOrderRemarks,
            "purchaser": purchaser,
            "purchaseDetails": purchaseDetails};

        $.ajax({
            type: 'POST',
            url: '/purchase/purchaseOrder',
            dataType: "json",
            data: JSON.stringify(purchaseOrder),
            contentType: "application/json;charset=utf-8",
            success: function (data) {
                $(window).attr("location","/purchaseOrders");
            }
        })

    }
</script>

</body>

</html>